package com.ppgames.gm.gmsystem.controller;

import com.ppgames.gm.gmsystem.GmCore;
import com.ppgames.gm.gmsystem.util.DBConnectionUtil;
import com.ppgames.gm.gmsystem.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.sql.DataSource;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * demo controller
 *
 * @author Joeg
 * @email 840459673@qq.com
 * @date 2018/4/19
 */
@RestController
@RequestMapping("/api/demo")
public class DemoController {


//    @Autowired
//    @Qualifier("entityManagerFactory")
    private EntityManager em;

    private EntityManagerFactory emf;

    private static DataSource dataSource;


    private static DataSource getDataSource() {
        if (dataSource == null) {
            dataSource = (DataSource) GmCore.applicationContext.getBean("dataSource");
        }
        return dataSource;
    }


    @PostMapping("")
    public String demo() {
        String sql = "SELECT id , cardidarray FROM app_starwork_93.starwork_user  WHERE cardidarray  <> ''";
        Query query = em.createNativeQuery(sql);
        List<UserVO> userVOS = DBHelper.getResultList(query, UserVO.class);

        userVOS.parallelStream().forEach(j -> {
            long a = j.getId().longValue() % 10;
            if (a != 0 && a != 2) {
                update(j);
            }
        });


        return "success";
    }

    private void update(UserVO j) {

        Connection connection = null;
        dataSource = getDataSource();
        try {
            String sql = "update  app_starwork_93.starwork_user  set cardidarray = ? WHERE id= ?";
            connection = dataSource.getConnection();
            connection.setAutoCommit(false);

            String[] cardiarrayStrs = j.getCardidarray().split(",");
            String separator = ",";
            StringBuilder sb = new StringBuilder();
            Stream.of(cardiarrayStrs).forEach(item -> {
                        long tmp = Long.valueOf(item);
                        if (tmp < 50000) {
                            sb.append(tmp + 50000).append(separator);
                        } else {
                            sb.append(tmp).append(separator);
                        }
                    }
            );
            if (cardiarrayStrs.length != 1) {
                sb.deleteCharAt(sb.length() - separator.length());
            }
            DBConnectionUtil.executeUpdate(connection, sql, Arrays.asList(sb.toString(), j.getId()));
            connection.commit();
            connection.setAutoCommit(true);

        } catch (SQLException e) {
            e.printStackTrace();
            DBConnectionUtil.rollback(connection);
        } finally {
            DBConnectionUtil.closeConnection(connection);
        }
    }


    public static class UserVO {
        private BigInteger id;
        private String cardidarray;

        public UserVO(BigInteger id, String cardidarray) {
            this.id = id;
            this.cardidarray = cardidarray;
        }

        public BigInteger getId() {
            return id;
        }

        public void setId(BigInteger id) {
            this.id = id;
        }

        public String getCardidarray() {
            return cardidarray;
        }

        public void setCardidarray(String cardidarray) {
            this.cardidarray = cardidarray;
        }
    }
}
